package com.shy.base.service.impl;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.shy.base.dao.BaseDao;
import com.shy.base.service.BaseService;
import com.shy.base.util.ObjectToWhereHql;
import com.shy.base.vo.AppConstants;
import com.shy.base.vo.PagerFilter;
import com.shy.base.vo.QueryParamVo;
import com.shy.base.vo.QueryResult;

@Transactional
public class BaseServiceImpl<T> implements BaseService<T> {

	public BaseDao<T> baseDao;
	
	public BaseDao<T> getBaseDao() {
		return baseDao;
	}
	public void setBaseDao(BaseDao<T> baseDao) {
		this.baseDao = baseDao;
	}

	public void delete(T entity) {
		baseDao.delete(entity);
	}

	public void deleteById(Serializable id) {
		baseDao.deleteById(id);
	}

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public List<T> findAll() {
		return baseDao.findAll();
	}
	
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public List<T> findByIds(Serializable[] ids) {
		return baseDao.findByIds(ids);
	}

	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public T getById(Serializable id) {
		return baseDao.getById(id);
	}

	public void save(T entity) {
		baseDao.save(entity);
	}

	public void update(T entity) {
		baseDao.update(entity);
	}
	
	public void batchDelete(Collection<T> entitys) {
		baseDao.batchDelete(entitys);
	}
	
	public void batchDeleteByIds(Collection<? extends Serializable> ids) {
		baseDao.batchDeleteByIds(ids);
	}
	
	public void batchSave(Collection<T> entitys) {
		baseDao.batchSave(entitys);
	}
	
	public void batchUpdate(Collection<T> entitys) {
		baseDao.batchUpdate(entitys);
	}
	
	public void deleteByIds(Serializable... ids) {
		baseDao.deleteByIds(ids);
	}
	
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public long getCount() {
		return baseDao.getCount();
	}
	
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public QueryResult<T> getScrollData(Integer firstindex,Integer maxresult) {
		return baseDao.getScrollData(firstindex,maxresult);
	}
	
	@Transactional(readOnly=true,propagation=Propagation.NOT_SUPPORTED)
	public QueryResult<T> getScrollData(Integer firstindex,Integer maxresult,
			String wherejql, Object[] queryParams,LinkedHashMap<String, String> orderby) {
		return baseDao.getScrollData(firstindex,maxresult,wherejql,queryParams,orderby);
	}
	
	@Override
	public PagerFilter listByPager(T entity, Map<String, String[]> fieldFilterMap, PagerFilter pagerFilter) {
		QueryParamVo queryParamVo = null;
		if (null != entity) {
			queryParamVo = ObjectToWhereHql.buildWhereHql(entity, null,
					null == fieldFilterMap  || fieldFilterMap.isEmpty() ? null : fieldFilterMap.get(AppConstants.UNUSED_FIELDS),
					null == fieldFilterMap  || fieldFilterMap.isEmpty() ? null : fieldFilterMap.get(AppConstants.LIKE_TYPES));
		}
		Integer firstStart = Integer.parseInt(String.valueOf(pagerFilter.getStart())); // 分页开始下标
		Integer showNum = Integer.parseInt(String.valueOf(pagerFilter.getMax())); // 显示条数
		QueryResult<T> result = baseDao.getScrollDataBindName(firstStart, showNum,
				null == queryParamVo ? null : queryParamVo.getWhereHql(),
				null == queryParamVo ? null : queryParamVo.getParams(), pagerFilter.getOrderby(), pagerFilter.getGroupBy());
		if (result != null) {
			pagerFilter.setTotal(result.getTotalrecord());
			pagerFilter.setObjs(result.getResultlist());
		}
		return pagerFilter;
	}
	
	@Override
	public T getByProperty(String propertyName, Object value) {
		return baseDao.getByProperty(propertyName, value);
	}
	
	@Override
	public T getByProperty(Map<String, Object> propertyMap) {
		return baseDao.getByProperty(propertyMap);
	}
	
	
	@Override
	public List<T> findByProperty(String propertyName, Object value) {
		return baseDao.findByProperty(propertyName, value);
	}
	
	@Override
	public List<T> findByProperty(String propertyName, Object value, String orderField, String orderDirect) {
		return baseDao.findByProperty(propertyName, value, orderField, orderDirect);
	}
	
	
	@Override
	public List<T> findByProperty(Map<String, Object> propertyMap) {
		return baseDao.findByProperty(propertyMap);
	}

	@Override
	public List<T> findByEntity(T entity, Map<String, String[]> fieldFilterMap, LinkedHashMap<String, String> orderBy) {
		PagerFilter pagerFilter = new PagerFilter();
		pagerFilter.setStart(0L);
		pagerFilter.setMax(-1L);
		pagerFilter = listByPager(entity, fieldFilterMap, pagerFilter);
		return pagerFilter.getObjs();
	}
    @Override
    public PagerFilter listByPagerMini(T entity, String[] fieldFilterArr, PagerFilter pagerFilter) {
        // TODO Auto-generated method stub
        return null;
    }
	
}
